package Regex;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo6 {
    public static void main(String[] args) throws IOException {
        //网络爬虫


    /*扩展需求2:
    把连接: https: //m.sengzan.com/jiaoyu/29104.html?ivk_sa=1025883i中所有的身份证号码都爬取出来。*/
        //创建一个URL对象
        URL url = new URL("https://www.bilibili.com");
        //连接上这个网址
        //细节:保证网络是畅通

        URLConnection conn = url.openConnection();//创建一个对象去读取网络中的数据
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;

        //获取正则表达式的对象
        String regex = "(((ht|f)tps?)://)?[\\w-]+(\\.[\\w-]+)+([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?";
        Pattern pattern = Pattern.compile(regex);

        //在读取的时候每次读一整行
        while ((line = br.readLine()) != null) {
            //拿着文本匹配器的对象matcher按照pattern的规则去读取当前的这一行信息
            Matcher matcher = pattern.matcher(line);
            while(matcher.find( )){
             System.out.println( matcher.group());
            }
        }
        br.close();
    }
}

